package com.hdu.mysql;

import com.hdu.context.FullLinkContext;
import com.hdu.context.FullLinkContextHolder;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * 动态数据源
 */
public class FullLinkDynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        FullLinkContext fullLinkContext = FullLinkContextHolder.getFullLinkContext();
        if (fullLinkContext != null) {
            // 压测数据 使用影子库
            DynamicDataSourceContextHolder.setDataSourceKey(DataSourceKey.SHADOW_DB);
        }
        else {
            DynamicDataSourceContextHolder.setDataSourceKey(DataSourceKey.PRIMARY_DB);
        }
        return DynamicDataSourceContextHolder.getDataSourceKey();
    }
}
